package com.gitee.zw.irmp.common.config.redis;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;

@Slf4j
@Configuration
@ConditionalOnProperty(value = "spring.data.redis.mode", havingValue = "cluster", matchIfMissing = false)
public class RedisClusterConfig {
    @Bean
    public RedisClusterConfiguration redisClusterConfiguration(RedisProperties prop) {
        log.info("Redis in cluster mode: {}", prop.getCluster().getNodes());
        RedisClusterConfiguration conf = new RedisClusterConfiguration(prop.getCluster().getNodes());
        BeanUtils.copyProperties(prop, conf);
        conf.setPassword(prop.getPassword());
        conf.setMaxRedirects(prop.getCluster().getMaxRedirects());
        return conf;
    }

//    @Bean
//    public LettuceConnectionFactory lettuceConnectionFactory(RedisClusterConfiguration redisClusterConfiguration) {
//        return new LettuceConnectionFactory(redisClusterConfiguration);
//    }
}
